from pav1_lexer import lex
outfile = open("ex.txt.py", "w")
file = open("ex.txt", "r")
lines = file.readlines()
file.close()

arr = list()

functions = ["ucase", "print", "lcase"]

def create_assign_var(var_name,assign_list): ## creates variable assignment part
    after_equal = ""
    for i in range (len(assign_list)):
        after_equal += assign_list[i]
    output = var_name + " = " + after_equal
    return output

def create_func_var(var_name , func_list): ## used when functions are used

    output = ""
    for i in range(len(func_list)):
        if func_list[i] == "ucase":
            output += var_name + "=" + var_name + ".upper()" +"\n"
        elif func_list[i] == "lcase":
            output += var_name + "=" + var_name + ".lower()" +"\n"
        elif func_list[i] == "print":
            output += "print(" + var_name + ")" +"\n"
    return output


def parser(list):
    type_list = []  # contains type of data
    name_list = []  # value of data
    output = ""
    for i in range(0, len(list)):
        type_list.append(list[i][0])
        name_list.append(list[i][1])

    if type_list[0] == "variable":
        assign_list = []
        func_list = []
        var_name = name_list[0]
        for i in range (1,len(name_list)):
            if type_list[i] != "function":
                assign_list.append(name_list[i]) ## stores assignment part
        for i in range (len(assign_list),len(name_list)):
            if type_list[i] == "function":
                func_list.append(name_list[i]) ##stores the functions used
        #print(func_list)


        if len(assign_list) > 0 and len(func_list) == 0: ## when no functions are used
            final_var = create_assign_var(var_name, assign_list)
            return final_var

        if len(assign_list) == 0 and len(func_list) > 0: ## when theres no assignment and functions are used
            output = create_func_var(var_name,func_list)
            return output

        if len(assign_list) > 0 and len(func_list) > 0: ## when both assignment is done and function(s) is used
            final_var = create_assign_var(var_name, assign_list)
            func_part = create_func_var(var_name,func_list)
            output = final_var + "\n" + func_part
            return output


        if len(assign_list) == 0 and len(func_list)==0: ## when theres no assignment and function call
            return (var_name+ " = 0 ")




for line in lines:
    line = line.strip()
    if line == "":
        pass
    else :
        arr.append(line)


for i in range (0,len(arr)):
    output = ( parser((lex(arr[i])))  )+"\n"
    outfile.write(output)

#output = create_func_var("var",["print","lcase","ucase"])
#print(output)